package com.zjjh.algorithm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.zjjh.algorithm.entity.Dictionary;
import com.zjjh.algorithm.entity.Userinfo;
import com.zjjh.algorithm.service.DictionaryService;
import com.zjjh.algorithm.service.UserinfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/base")
public class UserinfoController {

    @Autowired
    private UserinfoService userinfoService;

    @Autowired
    private DictionaryService dictionaryService;

    @GetMapping("/add")
    Object add() {
        for (int i = 0; i < 100; i++) {
            Userinfo userinfo = new Userinfo();
            String id = IdWorker.getIdStr();
            String code = RandomStringUtils.randomNumeric(8) + StringUtils.substring(id,15);
            userinfo.setAvatar("123123213");
            userinfo.setGender(1).setId(id).setIdCode(code).setNickname("1234123123123123").setMobile("1856465645").setRealname("12321312312").setDeleted(0);
            userinfoService.save(userinfo);
        }
        return "ok";
    }

    @GetMapping("/get")
    Object get() {
        return userinfoService.getById("1550353684061499393");
    }

    @GetMapping("/getList")
    Object getList() {
        List<String> a = new ArrayList<>();
        a.add("1550353684061499393");
        a.add("1550354218277416963");
        a.add("1550353683671429122");
        return userinfoService.listByIds(a);
    }

    @GetMapping("/query")
    Object query() {
        QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("realname","213213");
        return userinfoService.list(queryWrapper);
    }

    @GetMapping("/getByCode")
    Object getByCode() {
        QueryWrapper<Userinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_code","405057534035");
        return userinfoService.getOne(queryWrapper);
    }

    @GetMapping("/dic")
    Object dic() {
        Dictionary dictionary = new Dictionary();
        dictionary.setDictName("3213232").setDictOption("2132").setDictType("23213").setDictOptionValue("12312321").setDeleted(0);
        dictionaryService.save(dictionary);
        return dictionaryService.getById(dictionary.getId());
    }

}
